﻿package cocoaas3.flash{

	import cocoaas3.*;
	import flash.display.*;
	import flash.text.*;
	import flash.events.*;

	public class UIView extends Sprite {

		//
		// original member
		//
		//private var _subviews:Vector.<UIView> = new Vector.<UIView>();
		private var _superview:UIView;

		public var frame:CGRect;
		public var bounds:CGRect;

		// A Boolean value that determines whether subviews are confined to the bounds of the receiver.
		// true にすると、subview をこの UIView のサイズにクリップするが、現状 starling は対応していません。
		public var clipsToBounds:Boolean = false;

		// The receiver’s background color.
		// null で透明化
		public function set backgroundColor( val:Object ):void{
			opaqueBackground = val;
		}
		public function get backgroundColor():Object{
			return opaqueBackground;
		}
		
		// The receiver’s immediate subviews. (read-only)
		public function get subviews():Vector.<UIView>{
			var vec:Vector.<UIView> = new Vector.<UIView>();
			var len:uint = subviewContainer.numChildren;
			for( var i:int=0; i<len; i++ ){
				//trace( subviewContainer.getChildAt(i), subviewContainer.getChildAt(i).parent, subviewContainer.getChildAt(i).parent.parent );
				vec.push( subviewContainer.getChildAt(i).parent.parent );
			}
			return vec;
		}

		// The receiver’s superview, or nil if it has none. (read-only)
		public function get superview():UIView{
			return _superview;
		}

		
		

		//
		// custom member
		//
		internal var subviewContainer:Sprite = new Sprite();// UIScrollViewなどで使う
		internal var background_mc:Sprite = new Sprite();


		public function UIView(){
			this.addEventListener( Event.ADDED_TO_STAGE, onAddedToStage );
			this.addEventListener( Event.REMOVED_FROM_STAGE, onRemovedFromStage );

			// UIScrollViewなどのタッチ判定用に、透明の背景を作る
			//var shape = new Shape();
			//var g:Graphics = shape.graphics;
			//g.beginFill( 0xFF0000, 0 );	// 面のスタイル設定
			//g.drawRect( 0, 0, 100, 100 );

			//background_mc.addChild(shape);
			//addChild( background_mc );
			addChild( subviewContainer );
		}



		//
		// original functions
		//
		public function addSubview( view:DisplayObject ):void{
			//view._superview = this;
			//_subviews.push( view );
			subviewContainer.addChild( view );
		}

		public function removeFromSuperview():void{
			this.parent.removeChild( this );
		}
		
		
		






		//
		// custom functions
		//
		internal function onAddedToStage(e:Event):void{}
		internal function onRemovedFromStage(e:Event):void{}

	}
}